package com.yanqu.road.utils.range;

import java.util.Random;

public class RangeInt {
	public int min = 0; // 下限
	public int max = 0; // 上限

	public RangeInt() {
		
	}

	public RangeInt(int min, int max) {
		setValue(min, max);
	}
	
	public RangeInt(int[] vals) {
		if (vals.length >= 2) {
			setValue(vals[0], vals[1]);
		}
	}
	
	public void setValue(int min, int max) {
		if (min > max) {
			throw new RuntimeException("min > max");
		}
		this.min = min;
		this.max = max;
	}
	
	@Override
	public String toString() {
		return new StringBuilder().append(min).append(",").append(max).toString();
	}
	
	@Override
	public int hashCode() {
		// 不能使用整形数组的hashCode()方法，因为new int[]，即使数组里的数据都一样，hashCode()的值也不一样的
		return toString().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (this == obj) {
			return true;
		}

		if (obj instanceof RangeInt) {
			RangeInt rangeInt = (RangeInt) obj;
			if (this.min == rangeInt.min && this.max == rangeInt.max) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 取[min,max]之间的随机值
	 * 
	 * @return
	 */
	public int random() {
		if (min < max) {
			Random random = new Random();
			if (min < 0) {
				int tempMin = 0;
				int tempMax = max - min;
				return random.nextInt((1 + tempMax) - tempMin) + min;
			}
			return random.nextInt((1 + max) - min) + min;
		}
		return min;
	}
}
